/* Teach Tool  - Structural Dynamics
 * Copyright (C) 2009: Fabian Gerold, www.fabiangerold.de
 *
 * This file may be licensed under the terms of of the
 * GNU General Public License Version 3 (the "GPL").
 * 
 * Software distributed under the License is distributed 
 * on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either 
 * express or implied. See the GPL for the specific language 
 * governing rights and limitations.
 *
 * You should have received a copy of the GPL along with this 
 * program. If not, go to http://www.gnu.org/licenses/gpl.html
 * or write to the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QStyleOption>

#include "Mass.h"
#include "EMSWidget.h"

Mass::Mass( EMSWidget* system )
{
	m_ems = system;
	m_scale = 0.5;
	m_dragable = true;
	setFlag(ItemIsMovable);
	setZValue(2);
	setAcceptsHoverEvents( true );
}

void Mass::setDisp( double disp )
{
	setPos( m_xpos_init + disp*100, m_ypos_init );
}
void Mass::setMass( double m )
{
	m_scale = 0.1 + m/50;
	update();
}

void Mass::initPos( qreal x, qreal y )
{
	m_xpos_init = x;
	m_ypos_init = y;
	setPos( x, y );
}

QRectF Mass::boundingRect() const
 {
     return QRectF( -55*m_scale, -55*m_scale, 110*m_scale, 110*m_scale );
 }


 void Mass::paint( QPainter *painter, const QStyleOptionGraphicsItem*, QWidget* )
 {
	QRadialGradient gradient( 0, 0, 50*m_scale );
	gradient.setColorAt(0, Qt::white);
	gradient.setColorAt(0.7, QColor( 203, 205, 206 ) );
	gradient.setColorAt(1, QColor( 148, 150, 159 ) );

	painter->setBrush(gradient);
	painter->setPen(Qt::NoPen);
	painter->drawEllipse(-50*m_scale, -50*m_scale, 100*m_scale, 100*m_scale);

	// optimize: pixmap buffering
}

void Mass::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
	if( !m_dragable ) return;
	m_ems->pressMass();
	update();
	setCursor( QCursor( Qt::ClosedHandCursor ) );
	QGraphicsItem::mousePressEvent(event);
}
void Mass::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
	if( !m_dragable ) return;
	update();
	setCursor( QCursor( Qt::OpenHandCursor ) );
	QGraphicsItem::mouseReleaseEvent(event);
	m_ems->releaseMass( ( pos().x() - m_xpos_init )*0.01 );
}
void Mass::mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
{
	if( !m_dragable ) return;
	setCursor( QCursor( Qt::OpenHandCursor ) );
	
	QPointF diff_parent = mapToParent(event->pos()) - mapToParent(event->lastPos());
	QPointF diff = event->pos() - event->lastPos();

	qreal dx = pos().x() + diff.x() - m_xpos_init;
	if( dx <= 100 && dx >= -100 ) moveBy(diff.x(), 0);
	m_ems->dragMass( (pos().x()-m_xpos_init)*0.01 );
}
void Mass::hoverEnterEvent( QGraphicsSceneHoverEvent * event )
{
	if( !m_dragable ) return;
	setCursor( QCursor( Qt::OpenHandCursor ) );
	QGraphicsItem::hoverEnterEvent(event);
}
void Mass::hoverLeaveEvent( QGraphicsSceneHoverEvent * event )
{
	if( !m_dragable ) return;
	setCursor( QCursor( Qt::ArrowCursor ) );
	QGraphicsItem::hoverLeaveEvent(event);
}
